﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace XnaXaml.Controls
{
    public class ContainerControl : Control
    {
        #region Properties
        
        public List<Control> Children = new List<Control>();

        #endregion

        #region Constructor
        
        public ContainerControl() { }
        public ContainerControl(XNode xmlNode) : base(xmlNode)
        {
            GetChildren();
        }

        #endregion

        #region Methods
        
        public virtual void GetChildren()
        {
            var elements = (from element in (XmlNode as XElement).Elements() select element).ToList();
            
            if (elements.Count <= 0) return;
            Children = new List<Control>();
            for (var i = 0; i < elements.Count; i++)
            {
                var child = elements[i];

                var ctrl = new Control();
                switch ((child as XElement).Name.LocalName)
                {
                    case "Border":
                        ctrl = new Border(child);
                        break;
                    case "Grid":
                        ctrl = new Grid(child);
                        break;
                    case "Page":
                        ctrl = new Page(child);
                        break;
                    case "Button":
                        ctrl = new Button(child);
                        break;
                    case "CheckBox":
                        ctrl = new CheckBox(child);
                        break;
                    case "ComboBox":
                        ctrl = new ComboBox(child);
                        break;
                    case "Image":
                        ctrl = new Image(child);
                        break;
                    case "Label":
                        ctrl = new Label(child);
                        break;
                    case "ListBox":
                        ctrl = new ListBox(child);
                        break;
                    case "RadioButton":
                        ctrl = new RadioButton(child);
                        break;
                    case "StackPanel":
                        ctrl = new StackPanel(child);
                        break;
                    case "TabControl":
                        ctrl = new TabControl(child);
                        break;
                    case "TabItem":
                        ctrl = new TabItem(child);
                        break;
                    case "TextBlock":
                        ctrl = new TextBlock(child);
                        break;
                    case "TextBox":
                        ctrl = new TextBox(child);
                        break;
                }

                Children.Add(ctrl);
            }
        }

        #endregion
    }
}
